using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using com.paypal.soap.api;
using IGlobalist.Cms.Common;

namespace IGlobalist.Cms
{
    public partial class ReviewOrder : BasePage
    {
        private GetExpressCheckoutDetailsResponseType details;
        protected void Page_Load(object sender, EventArgs e)
        {
            PayPalAPI api = new PayPalAPI();
            if (this.Token != null)
            {
                this.details = api.GetExpressCheckoutDetails(this.Token);
                AmountLiteral.Text = this.PaymentAmount;

                this.PayPalResponse = api.DoExpressCheckoutPayment(this.Token, this.details.GetExpressCheckoutDetailsResponseDetails.PayerInfo.PayerID, this.PaymentAmount, this.PaymentAction, Currency);

                string txn = Receipt.DoExpressCheckoutPaymentResponseDetails.PaymentInfo.TransactionID;
                this.DataBind();
            }
            else
            {
                string returnURL = Request.Url.GetLeftPart(UriPartial.Authority) + Response.ApplyAppPathModifier("ReviewOrder.aspx") + "?" + Constants.PAYMENT_AMOUNT_PARAM + "=" + this.PaymentAmount + "&" + Constants.PAYMENT_CURRENCY_PARAM + "=" + this.Currency;
                string cancelURL = returnURL.Replace("ReviewOrder", "TestPayment");
                string notifyURL =  Request.Url.GetLeftPart(UriPartial.Authority) + "Notify.aspx";                
                SetExpressCheckoutResponseType response = api.SetExpressCheckout(this.PaymentAmount, returnURL, cancelURL, this.PaymentAction, Currency);          


                string host = "www." + PaymentPro.GetMerchantAccount(PaymentSystem.Paypal).Environment + ".paypal.com";
                Response.Redirect("https://" + host + "/cgi-bin/webscr?cmd=_express-checkout&" + Constants.TOKEN_PARAM + "=" + response.Token + "&" + Constants.PAYMENT_NOTIFY_PARAM+"="+notifyURL);
            }
        }


        #region Properties
        private string RequestPath
        {
            get
            {
                return InsertCookielessSessionUrl(Request.Path, Request.ApplicationPath);
            }
        }

        private string InsertCookielessSessionUrl(string path, string applicationPath)
        {

            return path.Replace(applicationPath, applicationPath + (Session.IsCookieless ? "/(" + Session.SessionID + ")" : string.Empty));
        }

        protected string PaymentAmount
        {
            get
            {
                return this.Request.QueryString.Get(Constants.PAYMENT_AMOUNT_PARAM);
            }
        }

        protected string Token
        {
            get
            {
                return this.Request.QueryString.Get(Constants.TOKEN_PARAM);
            }
        }

        protected string FirstNameLiteralText
        {
            get
            {
                string text = ExpressCheckoutDetails.GetExpressCheckoutDetailsResponseDetails.PayerInfo.PayerName.FirstName;
                return text;
            }
        }

        protected string LastNameLiteralText
        {
            get
            {
                string text = ExpressCheckoutDetails.GetExpressCheckoutDetailsResponseDetails.PayerInfo.PayerName.LastName;
                return text;
            }
        }

        protected string StreetLiteralText
        {
            get
            {
                string text = ExpressCheckoutDetails.GetExpressCheckoutDetailsResponseDetails.PayerInfo.Address.Street1;
                return text;
            }
        }

        protected string CityLiteralText
        {
            get
            {
                string text = ExpressCheckoutDetails.GetExpressCheckoutDetailsResponseDetails.PayerInfo.Address.CityName;
                return text;
            }
        }

        protected string StateLiteralText
        {
            get
            {
                string text = ExpressCheckoutDetails.GetExpressCheckoutDetailsResponseDetails.PayerInfo.Address.StateOrProvince;
                return text;
            }
        }

        protected string PostalCodeLiteralText
        {
            get
            {
                string text = ExpressCheckoutDetails.GetExpressCheckoutDetailsResponseDetails.PayerInfo.Address.PostalCode;
                return text;
            }
        }

        protected string CountryLiteralText
        {

            get
            {
                string text = ExpressCheckoutDetails.GetExpressCheckoutDetailsResponseDetails.PayerInfo.Address.CountryName;
                return text;
            }
        }

        protected CurrencyCodeType Currency
        {
            get
            {
                return (CurrencyCodeType)Enum.Parse(typeof(CurrencyCodeType), this.Request.QueryString.Get(Constants.PAYMENT_CURRENCY_PARAM));
            }
        }

        protected GetExpressCheckoutDetailsResponseType ExpressCheckoutDetails
        {
            get
            {
                return this.details;
            }
        }

        protected DoExpressCheckoutPaymentResponseType Receipt
        {
            get
            {
                return (DoExpressCheckoutPaymentResponseType)this.PayPalResponse;
            }
        }

        #endregion

      
    }
}
